//
//  StatusTableViewCell.swift
//  DSWeibo
//
//  Created by liuzhu on 2018/11/24.
//  Copyright © 2018 liuzhu. All rights reserved.
//

import UIKit

class StatusTableViewCell: UITableViewCell {

    var status:Status?{
        didSet{
            //textLabel?.text = status?.text
            nameLabel.text = status?.user?.name
            contentLabel.text = status?.text
        }
    }
    
    override init(style: UITableViewCell.CellStyle, reuseIdentifier: String?) {
        super.init(style:style,reuseIdentifier:reuseIdentifier)
        setupUI()
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    private lazy var headImageView:UIImageView = {
        let iv = UIImageView(image:UIImage(named: "avatar_default_big"))
        return iv
    }()
    
    private lazy var verifiedView:UIImageView = {
        let iv = UIImageView(image: UIImage(named: "avatar_enterprise_vip"))
        return iv
    }()
    
    private lazy var nameLabel:UILabel = {
        let iv = UILabel()
        iv.textColor = UIColor.darkGray
        iv.font = UIFont.systemFont(ofSize: 14)
        return iv
    }()
    
    private lazy var vipView:UIImageView = {
        let iv = UIImageView(image: UIImage(named: "common_icon_membership"))
        return iv
    }()
    
    private lazy var timeLabel:UILabel = {
        let iv = UILabel()
        iv.textColor = UIColor.darkGray
        iv.font = UIFont.systemFont(ofSize: 14)
        iv.text = "刚刚"
        return iv
    }()
    
    private lazy var sourceLabel:UILabel = {
        let iv = UILabel()
        iv.textColor = UIColor.darkGray
        iv.font = UIFont.systemFont(ofSize: 14)
        iv.text = "来自：辽宁号"
        return iv
    }()
    
    private lazy var contentLabel:UILabel = {
        let iv = UILabel()
        iv.textColor = UIColor.darkGray
        iv.font = UIFont.systemFont(ofSize: 14)
        iv.numberOfLines = 0    //多行
        iv.preferredMaxLayoutWidth = UIScreen.main.bounds.width - 20 //最大宽度
       
        return iv
    }()
    
    private lazy var footerView:StatusFooterView = {
        let iv = StatusFooterView()
        return iv
    }()
    
    private func setupUI(){
        contentView.addSubview(headImageView)
        contentView.addSubview(verifiedView)
        contentView.addSubview(nameLabel)
        contentView.addSubview(vipView)
        contentView.addSubview(timeLabel)
        contentView.addSubview(sourceLabel)
        contentView.addSubview(contentLabel)
        contentView.addSubview(footerView)
        
        headImageView.xmg_AlignInner(type: XMG_AlignType.TopLeft, referView: contentView, size: CGSize(width: 50, height: 50), offset: CGPoint(x: 10, y: 10))
        verifiedView.xmg_AlignInner(type: XMG_AlignType.BottomRight, referView: headImageView, size: CGSize(width: 14, height: 14), offset: CGPoint(x: 5, y: 5))
        nameLabel.xmg_AlignHorizontal(type: XMG_AlignType.TopRight, referView: headImageView, size: nil, offset: CGPoint(x: 10, y: 0))
        vipView.xmg_AlignHorizontal(type: XMG_AlignType.TopRight, referView: nameLabel, size: CGSize(width: 14, height: 14), offset: CGPoint(x: 10, y: 0))
        timeLabel.xmg_AlignHorizontal(type: XMG_AlignType.BottomRight, referView: headImageView, size: nil, offset: CGPoint(x: 10, y: 0))
        sourceLabel.xmg_AlignHorizontal(type: XMG_AlignType.BottomRight, referView: timeLabel, size: nil, offset: CGPoint(x: 10, y: 0))
        contentLabel.xmg_AlignVertical(type: XMG_AlignType.BottomLeft, referView: headImageView, size: nil, offset: CGPoint(x: 0, y: 20))
//        //约束contentlabel和cell底部的距离
//        contentLabel.xmg_AlignInner(type: XMG_AlignType.BottomRight, referView: contentView, size: nil, offset: CGPoint(x: -10, y: -10))
        let width = UIScreen.main.bounds.size.width
        footerView.xmg_AlignVertical(type: XMG_AlignType.BottomLeft, referView: contentLabel, size: CGSize(width: width, height: 40), offset: CGPoint(x: -10, y: 5))
        footerView.xmg_AlignInner(type: XMG_AlignType.BottomRight, referView: contentView, size: nil, offset: CGPoint(x: -10, y: -5))
    }
}

class StatusFooterView: UIView {
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        setupUI()
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    private func setupUI(){
        addSubview(retweetBtn)
        addSubview(unlikeBtn)
        addSubview(commonBtn)
        xmg_HorizontalTile(views: [retweetBtn,unlikeBtn,commonBtn], insets: UIEdgeInsets(top: 0, left: 0, bottom: 0, right: 0))
    }
    
    private lazy var retweetBtn:UIButton = {
        let btn = UIButton()
        btn.setImage(UIImage(named: "timeline_icon_retweet"), for: UIControl.State.normal)
        btn.setTitleColor(UIColor.lightGray, for: UIControl.State.normal)
        btn.titleLabel?.font = UIFont.systemFont(ofSize: 13)
        btn.setTitle("转发", for: UIControl.State.normal)
        return btn
    }()
    
    private lazy var unlikeBtn:UIButton = {
        let btn = UIButton()
        btn.setImage(UIImage(named: "timeline_icon_unlike"), for: UIControl.State.normal)
        btn.setTitleColor(UIColor.lightGray, for: UIControl.State.normal)
        btn.titleLabel?.font = UIFont.systemFont(ofSize: 13)
        btn.setTitle("赞", for: UIControl.State.normal)
        return btn
    }()
    
    private lazy var commonBtn:UIButton = {
        let btn = UIButton()
        btn.setImage(UIImage(named: "timeline_icon_comment"), for: UIControl.State.normal)
        btn.setTitleColor(UIColor.lightGray, for: UIControl.State.normal)
        btn.titleLabel?.font = UIFont.systemFont(ofSize: 13)
        btn.setTitle("评论", for: UIControl.State.normal)
        return btn
    }()

}
